package com.ruoyi.web.controller.crm;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.CrmFollowUp;
import com.ruoyi.system.domain.CrmFollowUpVo;
import com.ruoyi.system.domain.CrmStudentVo;
import com.ruoyi.system.service.ICrmFollowUpService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CrmStudent;
import com.ruoyi.system.service.ICrmStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学员信息Controller
 * 
 * @author Bill
 * @date 2024-06-22
 */
@Controller
@RequestMapping("/system/student")
public class CrmStudentController extends BaseController
{
    private String prefix = "system/student";

    @Autowired
    private ICrmStudentService crmStudentService;

    @Autowired
    private ICrmFollowUpService crmFollowUpService;

    @RequiresPermissions("system:student:view")
    @GetMapping()
    public String student(ModelMap mmap)
    {
        SysUser queryUser = new SysUser();
        queryUser.setStatus("0");
        mmap.put("users", crmStudentService.selectUserList(queryUser));

        SysUser queryDisabledUser = new SysUser();
        queryDisabledUser.setStatus("1");
        mmap.put("disabledUsers", crmStudentService.selectUserList(queryDisabledUser));

        SysUser querySalesman = new SysUser();
        querySalesman.setSaleStatus("0");
        mmap.put("salesmanList", crmStudentService.selectSalesmanList(querySalesman));

        SysUser queryPromotion = new SysUser();
        mmap.put("promotionList", crmStudentService.selectPromotionList(queryPromotion));
        return prefix + "/student";
    }

    /**
     * 查询学员信息列表
     */
    @RequiresPermissions("system:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CrmStudent crmStudent)
    {
        startPage();

        List<CrmStudent> list = crmStudentService.selectCrmStudentList(crmStudent);
        TableDataInfo rspData = getDataTable(list);
        crmStudentService.selectCrmStudentVoList(rspData);
        return rspData;
    }

    /**
     * 导出学员信息列表
     */
    @RequiresPermissions("system:student:export")
    @Log(title = "学员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CrmStudent crmStudent)
    {
        List<CrmStudent> list = crmStudentService.selectCrmStudentList(crmStudent);
        ExcelUtil<CrmStudent> util = new ExcelUtil<CrmStudent>(CrmStudent.class);
        return util.exportExcel(list, "学员信息数据");
    }

    /**
     * 新增学员信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
//        SysUser queryUser = new SysUser();
//        queryUser.setStatus("0");
//        mmap.put("users", crmStudentService.selectUserList(queryUser));

        SysUser querySalesman = new SysUser();
        querySalesman.setSaleStatus("0");
        mmap.put("salesmanList", crmStudentService.selectSalesmanList(querySalesman));

        SysUser queryPromotion = new SysUser();
        mmap.put("promotionList", crmStudentService.selectPromotionList(queryPromotion));

        return prefix + "/add";
    }

    /**
     * 新增保存学员信息
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "学员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CrmStudent crmStudent)
    {
        crmStudent.setCreateUser(getUserId());
        return toAjax(crmStudentService.insertCrmStudent(crmStudent));
    }

    /**
     * 修改学员信息
     */
    @RequiresPermissions("system:student:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CrmStudent crmStudent = crmStudentService.selectCrmStudentById(id);
        mmap.put("crmStudent", crmStudent);

        SysUser querySalesman = new SysUser();
        querySalesman.setSaleStatus("0");
        mmap.put("salesmanList", crmStudentService.selectSalesmanList(querySalesman, crmStudent.getSalesmanId()));

        SysUser queryPromotion = new SysUser();
        mmap.put("promotionList", crmStudentService.selectPromotionList(queryPromotion, crmStudent.getResourceUserId()));

        return prefix + "/edit";
    }

    /**
     * 查询用户详细
     */
    @RequiresPermissions("system:student:list")
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        CrmStudentVo studentVo = crmStudentService.selectCrmStudentVoById(id);

        mmap.put("crmStudent", studentVo);

        mmap.put("users", crmStudentService.selectUserList(new SysUser()));

        CrmFollowUp followUp = new CrmFollowUp();
        followUp.setStudentId(id);

        List<CrmFollowUp> list = crmFollowUpService.selectCrmFollowUpList(followUp);
        TableDataInfo rspData = getDataTable(list);
        crmFollowUpService.selectCrmFollowUpVoList(rspData);
        mmap.put("followUplist", rspData.getRows());
        return prefix + "/view";
    }


    /**
     * 修改保存学员信息
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "学员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CrmStudent crmStudent)
    {
        crmStudent.setUpdateUser(getUserId());
        return toAjax(crmStudentService.updateCrmStudent(crmStudent));
    }

    /**
     * 删除学员信息
     */
    @RequiresPermissions("system:student:remove")
    @Log(title = "学员信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(crmStudentService.deleteCrmStudentByIds(ids));
    }


    /**
     * 学员跟进状态修改
     */
    @Log(title = "学员管理-跟踪状态", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:student:edit")
    @PostMapping("/changeFollowStatus")
    @ResponseBody
    public AjaxResult changeFollowStatus(CrmStudent student)
    {
//        userService.checkUserAllowed(user);
//        userService.checkUserDataScope(user.getUserId());
        return toAjax(crmStudentService.changeFollowStatus(student));
    }

    /**
     * 学员收藏状态修改
     */
    @Log(title = "学员管理-收藏状态", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:student:edit")
    @PostMapping("/changeCollectStatus")
    @ResponseBody
    public AjaxResult changeCollectStatus(CrmStudent student)
    {
//        userService.checkUserAllowed(user);
//        userService.checkUserDataScope(user.getUserId());
        return toAjax(crmStudentService.changeCollectStatus(student));
    }
}
